<template>
  <div class="main">
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="工单编号">
          <el-input v-model="formInline.taskCode" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="工单状态">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option label="代办" value="1" />
            <el-option label="进行" value="2" />
            <el-option label="取消" value="3" />
            <el-option label="完成" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-button
        type="danger"
        icon="iconfont icon-zengjiatianjiajiahao"
        @click="dialogVisible=true"
      >新建</el-button>
      <el-button v-if="hiden" type="warning" plain @click.native="supplyValueFn">工单配置</el-button>
      <Table :list="list" @showIsChange="showIsChangeFn" />
    </div>
    <div class="fotter">
      <div class="left">
        {{ totalNum }}
      </div>
      <div class="block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          :background="true"
          layout="prev, next"
          @current-change="handleCurrentChange"
          @prev-click="formInline.index --"
          @next-click="formInline.index ++"
        />
      </div>
    </div>
    <el-dialog
      title="新增工单"
      :visible.sync="dialogVisible"
      width="40%"
      :close="handleClose"
      :close-on-click-modal="false"
    >
      <AddTask ref="addTask" :hiden="hiden" :detail="details" :task-id="taskId" @getTaskId="getTaskIdFn" @showTwoDialog="showTwoDialogFn" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button id="sure" type="primary" @click="addTaskFn">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="补货详情"
      :visible.sync="showInfo"
      width="40%"
      :before-close="handleClose2"
      :close-on-click-modal="false"
    >
      <TaskInfo v-if="isChange" :task-list="taskList" />
      <OperatorInfo v-else :order-list="orderList" @addCurrent="addCurrentFn" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="showInfo = false">取 消</el-button>
        <el-button type="primary" @click="showInfo = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="工单详情"
      :visible.sync="showInfo2"
      width="40%"
      :close-on-click-modal="false"
      @close="handleClose3"
    >
      <Card :obj="obj" @infoFn="getInfoFn" />
      <span slot="footer" class="dialog-footer">
        <el-button
          v-if="showButton"
          type="primary"
          @click="resetFn"
        >重新创建</el-button>
        <el-button v-else type="primary" @click="cancleFn">取消工单</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="工单配置"
      :visible.sync="showSubbly"
      width="40%"
      :close-on-click-modal="false"
    >
      <el-form :data="formValue">
        <el-form-item label="补货警戒线" required>
          <el-input-number v-model="formValue.alertValue" controls-position="right" :min="10" :max="100" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSubbly = false">取 消</el-button>
        <el-button type="primary" @click="subSubFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getTaskList, getAddTask, getSupplyValue, setSupplyValue, cancleTask } from '@/api'
import Table from './components/table.vue'
import AddTask from './components/addTask.vue'
import OperatorInfo from './components/operatorinfo.vue'
import Card from './components/card.vue'
import TaskInfo from './components/taskInfo.vue'
export default {
  components: {
    Table,
    AddTask,
    OperatorInfo,
    Card,
    TaskInfo
  },
  props: {
    hiden: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      formValue: {
        // 预警值
        alertValue: 0
      },
      // 选择显示button
      showButton: true,
      // 显示工单配置弹框
      showSubbly: false,
      // 切换显示
      isChange: false,
      details: [],
      showInfo: false,
      showInfo2: false,
      taskId: '',
      dialogVisible: false,
      totalNum: 0,
      formInline: {
        taskCode: '',
        status: '',
        pageIndex: 1,
        pageSize: 10
      },
      list: [],
      // 商品列表
      orderList: [],
      // 详情对象
      obj: {},
      // 回显的商品列表
      taskList: []

    }
  },
  created() {
    this.fetchTaskList()
  },
  methods: {
    // 取消工单
    cancleFn() {
      this.$confirm('此操作将永久删除工单, 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await cancleTask(this.obj)
        this.handleClose3()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //  点击工单确认按钮
    async subSubFn() {
      const res = await setSupplyValue(this.formValue.alertValue)
      res && this.$message.success('设置成功！')
    },
    // 点击工单配置按钮
    async supplyValueFn() {
      // console.log(1)
      this.showSubbly = true
      const res = await getSupplyValue()

      this.formValue.alertValue = res.data
    },
    // 商品回显
    getInfoFn(val) {
      this.taskList = val
      this.isChange = true
      this.showInfo = true
    },
    // 点击重新创建
    async resetFn() {
      this.dialogVisible = true
      // 调用接口，数据回显   获取到补货详情列表
      const { data } = await getAddTask(this.obj.taskId)
      // console.log(data)
      this.$nextTick(() => {
        // console.log(this.$refs.addTask.form)
        this.$refs.addTask.form = {
          assignorId: this.obj.userName,
          desc: this.obj.desc,
          innerCode: this.obj.innerCode,
          productType: this.obj.typeName,
          userId: this.$store.getters.id,
          details: data
        }
        // console.log(this.obj)
      })
    },
    // 点击查看详情
    showIsChangeFn(val) {
      this.showInfo2 = true
      this.showButton = val.taskStatusTypeEntity.statusName === '取消'
      this.obj = val
    },
    // 调用子组件方法，新增工单

    addTaskFn() {
      this.$refs.addTask.addTaskFn()
    },
    // 增加商品信息
    addCurrentFn(val) {
      this.details.push(val)
      console.log(val)
    },
    // 展示新增第二个弹出框
    showTwoDialogFn(val) {
      this.orderList = val
      this.isChange = false
      this.showInfo = true
      // console.log(val)
    },
    getTaskIdFn(id) {
      // console.log(id)
      const res = this.list.find(item => item.innerCode === id)
      // console.log(res)
      if (res) this.taskId = res.taskId
    },
    // 关闭弹窗
    handleClose() {
      this.dialogVisible = false
    },
    handleClose2() {
      this.showInfo = false
    },
    handleClose3() {
      this.showInfo2 = false
    },
    // 搜索接口
    async fetchTaskList(val = {}) {
      const { data } = await getTaskList(val)
      // console.log(data)
      this.totalNum = `共${data.totalCount}条记录  第${data.pageIndex}/${data.totalPage}页`
      this.list = data.currentPageRecords.map((item, index) => {
        item.index = this.formInline.pageIndex * this.formInline.pageSize - 9 + index
        item = { ...item, ...item.taskType }
        item.updateTime = item.updateTime.replace('T', ' ')
        item.createType = item.createType === 0 ? '自动' : '手动'
        return item
      })
    },
    onSubmit() {
      this.fetchTaskList(this.formInline)
    },
    // 当前页
    handleCurrentChange(val) {
      this.formInline.pageIndex = val
      this.fetchTaskList(this.formInline)
    }
  }
}
</script>

<style lang="scss" >
.main {
  padding: 20px;
  background-color: #f3f6fb;
  .header {
    box-sizing: border-box;
    width: 100%;
    height: 64px;
    padding-top: 17px;
    padding-left: 17px;
    margin-bottom: 20px;
    background-color: #fff;
  }
  .table,.fotter {
    padding: 15px;
    background-color: #fff;
  }
}
.fotter {
  display: flex;
  justify-content: space-between;
  .left {
    color: #d5ddf8;
  }
  .block  {
  span {
    width: 1400px;
    color: #dbdfe5;
  }
  button {
  span {
    width: 70px;
    margin: 0 10px;
    color: #333;
    background-color:#d5ddf8;
  }
}
}
}
.el-button.el-button--warning.is-plain{
  border: none;
  padding: 12px 20px;
  span {
    color: #999;
    font-size: 16px;
  }
}
.el-button.el-button--danger {
  text-align: center;
  .iconfont {
    margin-right: 5px;
  }
  span{
    font-size: 16px;
  }
}
.el-dialog__footer{
  text-align: center;
 .dialog-footer {
#sure {
  align-items: center;
    width: 80px!important;
    height: 36px;
    padding: 0;
    margin-left: 30px;
    background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
    border: none;
}
.fotter{
  background-color: #000;
}
}}
.el-input-number {
  width: 70%;
}
.el-form-item.is-required {
  margin-left: 20px;
}
</style>
